package com.ctshk.rpc.order.custom.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.rpc.order.custom.req.order.CustomOrderIdReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 定制包团订单 Provider 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-23
 */
public class CustomOrderProvider {

    public String selectCountBySelective(CustomOrderListReq req) {
        return new SQL(){{
            SELECT("sum(tso.total_people) as total_people, sum(tsoas.total_receivables) as total_receivables, " +
                    "sum(tsoas.total_paid_in) as total_paid_in, sum(tsoas.remaining_receivables) as remaining_receivables");
            FROM("t_customized_order tso");
            LEFT_OUTER_JOIN("t_customized_order_amount_statistics tsoas ON tso.id = tsoas.order_id");
            if(StringUtils.isNotBlank(req.getOrderNumber())) {
                WHERE("tso.order_number=#{orderNumber}");
            }
            if(StringUtils.isNotBlank(req.getCondition1())) {
                WHERE("((tso.contact_cn_surname || tso.contact_cn_name) like concat('%',#{condition1},'%') " +
                        "OR (tso.contact_en_surname || tso.contact_en_name) like concat('%',#{condition1},'%'))");
            }
            //默认查询30天内的统计
            LocalDate startTime = LocalDate.now().minusDays(30);
            LocalDate endTime = LocalDate.now();
            if (req.getStartTime() != null){
                startTime = req.getStartTime();
            }
            if (req.getEndTime() != null){
                endTime = req.getEndTime();
            }
            WHERE("tso.gmt_create BETWEEN cast('"+startTime+"' as DATE) AND cast('"+endTime+"' as DATE)");
        }}.toString();
    }

    public String selectListBySelective(IPage<CustomOrderListReq> page, CustomOrderListReq req,boolean permision, List<Long> idList) {
        return new SQL(){{
            SELECT("tso.id, tso.schedule_id, tso.create_id,tso.gmt_create, tso.order_number,tso.order_source,  " +
                    "(SELECT group_concat(CONCAT(temp_tsoti.tourist_type_name,'])',temp_tsoti.people) SEPARATOR '})')" +
                    "FROM(SELECT tsoti.main_data_id, tsoti.tourist_type_name, count(*) AS people FROM t_customized_order_tourist_info tsoti " +
                    "WHERE tsoti.order_id = tso.id GROUP BY tsoti.main_data_id ) temp_tsoti) as touristPeopleList, " +
                    "tsoas.total_receivables, tsoas.total_paid_in, tsoas.remaining_receivables, tsotr.refund_status, tsotq.refund_status AS quit_refund_status " +
                    "");
            FROM("t_customized_order tso");
            LEFT_OUTER_JOIN("t_customized_order_amount_statistics tsoas ON tso.id = tsoas.order_id");
            LEFT_OUTER_JOIN("( SELECT temp.order_id, temp.refund_status FROM t_customized_order_tourist_refund temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotr ON tsotr.order_id = tso.id");
            LEFT_OUTER_JOIN("( SELECT temp.order_id, temp.refund_status FROM t_customized_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.order_id = tso.id");
            WHERE("tso.is_deleted = 0");
            if(StringUtils.isNotBlank(req.getOrderNumber())) {
                WHERE("tso.order_number=#{req.orderNumber}");
            }
            if(StringUtils.isNotBlank(req.getCondition1())) {
                WHERE("((tso.contact_cn_surname || tso.contact_cn_name) like concat('%',#{req.condition1},'%') " +
                        "OR (tso.contact_en_surname || tso.contact_en_name) like concat('%',#{req.condition1},'%'))");
            }
            if (req.getIsOnlyMe() != null && Objects.equals(req.getIsOnlyMe(),1)){
                WHERE("tso.create_id = #{req.userId}");
            }
            if (req.getSalespersonId() != null){
                WHERE("tso.create_id = #{req.salespersonId}");
            }
           /* if (!permision && CollectionUtils.isNotEmpty(idList)){
                WHERE(" tso.create_id IN #{idList}");
            }*/
            ORDER_BY("tso.gmt_create desc");
        }}.toString();
    }

    public String selectDetailBySelective(CustomOrderIdReq req) {
        return new SQL(){{
            SELECT("tso.id,tso.schedule_id, tso.order_number,tso.schedule_price_type,tso.big_client_id,tso.big_client_name, " +
                    "tso.contact_cn_surname, tso.contact_cn_name, tso.contact_en_surname, tso.contact_en_name, " +
                    "tso.contact_gender, tso.contact_telephones, tso.contact_email, tso.internal_remark, tso.customer_type, "+
                    "(SELECT group_concat(CONCAT(temp_tsoti.tourist_type_name,'])',temp_tsoti.people,temp_tsoti.main_data_id) SEPARATOR '})') " +
                    "FROM(SELECT tsoti.main_data_id, tsoti.tourist_type_name, count(*) AS people FROM t_customized_order_tourist_info tsoti " +
                    "WHERE tsoti.order_id = tso.id GROUP BY tsoti.main_data_id ) temp_tsoti) as touristPeopleList, " +
                    "tsoas.total_receivables, tsoas.total_paid_in,tsoas.remaining_receivables, tsoas.refund_amount, tsoas.total_tour_fee, tso.total_people, " +
                    "tsoas.collection_fee, tsoas.adjustment_cost, tsoas.other_amount ");
            FROM("t_customized_order tso");
            LEFT_OUTER_JOIN("t_customized_order_amount_statistics tsoas ON tso.id = tsoas.order_id");
            WHERE("tso.id=#{id}");
        }}.toString();
    }

}
